package com.r2coding.gateway.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.r2coding.gateway.model.VisitNumModel;
import com.r2coding.gateway.util.GetCurrentWeekAllDateUtil;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:98k灬
 * @since:2022/06/06 22:15
 **/
@Service
public class RabbitListen {
    private static final String VISIT_KEY = "visit_num";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    // @RabbitListener(queues = {"IBlog_Visit"})
    public void visitListen(String date) {
        List<VisitNumModel> list = new ArrayList<>();
        boolean flag = false;
        if (stringRedisTemplate.hasKey(VISIT_KEY)) {
            String visitNumModelStr = stringRedisTemplate.opsForValue().get(VISIT_KEY);
            JSONArray array = JSONArray.parseArray(visitNumModelStr);
            List<VisitNumModel> finalList = list;
            for (Object o : array) {
                JSONObject obj = (JSONObject)o;
                String dateObj = (String)obj.get("date");
                if (dateObj.equals(date)) {
                    flag = true;
                }
            }

            if (flag) {
                array.forEach(i-> {
                    JSONObject o = (JSONObject)i;
                    String dateObj = (String)o.get("date");
                    Integer visitNum = (Integer)o.get("visitNum");
                    if (date.equals(dateObj)) {
                        finalList.add(new VisitNumModel(dateObj, visitNum + 1));
                    } else {
                        finalList.add(new VisitNumModel(dateObj, visitNum));
                    }
                });
                String s = JSON.toJSONString(finalList);
                stringRedisTemplate.opsForValue().set(VISIT_KEY, s);
            } else {
                this.createViewNum(date, array);
            }
        } else {
            this.createViewNum(date, null);
        }
    }

    // redisData不为空 : redis中有数据 但没有当日数据   2:redisData为空 :redis中无任何数据  一般是新启的服务 redis中还未存任何数据
    private void createViewNum(String dateFormat, JSONArray redisData) {
        List<String> dateList = GetCurrentWeekAllDateUtil.getAllDateWeek();
        System.out.println("dateList:" + dateList);
        List<VisitNumModel> list = new ArrayList<>();
        List<String> redisDateList = new ArrayList<>();
        if (redisData != null) {
            redisData.forEach( item -> {
                JSONObject o = (JSONObject)item;
                String dateObj = (String)o.get("date");
                redisDateList.add(dateObj);
            });

            boolean isCurrentDay = this.checkIsCurrentDay(dateList, redisDateList);

            List<VisitNumModel> finalList = list;
            redisData.forEach(item -> {
                JSONObject o = (JSONObject)item;
                String dateObj = (String)o.get("date");
                dateList.forEach( date -> {
                    if (date.equals(dateObj)) {
                        Integer visitNum = (Integer)o.get("visitNum");
                        VisitNumModel model = new VisitNumModel(date, visitNum);
                        finalList.add(model);
                    }
                    if (!isCurrentDay) {
                        VisitNumModel model = new VisitNumModel(dateFormat, 0);
                        finalList.add(model);
                    }
                });
            });
            String s = JSON.toJSONString(finalList);
            stringRedisTemplate.opsForValue().set(VISIT_KEY, s);
        } else {
            list = dateList.stream().map(item -> {
                VisitNumModel model = new VisitNumModel();
                if (item.equals(dateFormat)) {
                    model.setVisitNum(1);
                } else {
                    model.setVisitNum(0);
                }
                model.setDate(item);
                return model;
            }).collect(Collectors.toList());
            String s = JSON.toJSONString(list);
            stringRedisTemplate.opsForValue().set(VISIT_KEY, s);
        }
    }

    private boolean checkIsCurrentDay(List<String> list1, List<String> list2) {
        return list1.get(0).equals(list2.get(0)) && list1.get(list1.size() - 1).equals(list2.get(list2.size() - 1));
    }
}
